/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package JavaClass;

import Main.ExpoManagerMain;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Mai Lan
 */
public class Config {
   public    static  void SaveConfig(){

        String pass = new String(ExpoManagerMain.passPW.getPassword());
          try {
            File file= new File("server.config");
            if(!file.exists()){
                file.createNewFile();
                System.out.println("create file successful");
                ObjectOutputStream fos = new ObjectOutputStream(new FileOutputStream(file));

            Server sv = new Server(ExpoManagerMain.txtServer.getText(), Integer.parseInt(ExpoManagerMain.txtport.getText()), ExpoManagerMain.txtUser.getText(),pass, "ExpoManagement");
            fos.writeObject(sv);
            System.out.println(pass);
            fos.close();
            }else{
             ObjectOutputStream fos = new ObjectOutputStream(new FileOutputStream(file));

            Server sv = new Server(ExpoManagerMain.txtServer.getText(), Integer.parseInt(ExpoManagerMain.txtport.getText()), ExpoManagerMain.txtUser.getText(),pass, "ExpoManagement");
            fos.writeObject(sv);
            System.out.println(pass);
            fos.close();
            ExpoManagerMain.Connect();
            }
          Config.configServer();
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
   
    public static void configServer() {
        FileInputStream fi = null;
        ObjectInputStream oi = null;
        try {
             File file= new File("server.config");
            if(!file.exists()){
                file.createNewFile();
            }
            fi = new FileInputStream("server.config");
            oi = new ObjectInputStream(fi);
            if (oi != null) {

                Server sv = (Server) oi.readObject();
//                databaseName = sv.getDatabaseName();
                ExpoManagerMain.ServerName = sv.getServerName();
                ExpoManagerMain.port = sv.getPort();
                ExpoManagerMain.Username = sv.getUserName();
                ExpoManagerMain.Password = sv.getPassword();
                ExpoManagerMain.url = "jdbc:sqlserver://" + ExpoManagerMain.ServerName + ":" + ExpoManagerMain.port + ";databaseName=ExpoManagement" ;
            } else {
                System.out.println("error occur when connection database!");
            }
            oi.close();
            fi.close();

        } catch (IOException ex) {
            System.out.println("not found 'server.config' file");
            


        } catch (ClassNotFoundException ex) {
            System.out.println("error");
        } catch (Exception ex) {
            System.out.println("not found 'server.config' file");
        }
    }
}
